package com.greenzebras.geocache;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.greenzebras.geocache.cachedata.Cache;
import com.greenzebras.geocache.constant.GeocacheConstants;
import com.greenzebras.geocache.datastore.GeocacheCacheManagementRequestResponseListener;

public class GeocacheDetailActivity extends Activity implements
		GeocacheCacheManagementRequestResponseListener {

	static final int EDIT_CACHE_REQUEST = 0;

	public static final String CACHE_TO_DISPLAY = "cacheToDisplay";
	private Cache cache = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);
		((TextView) findViewById(R.id.activity_title)).setText(R.string.detail);

		if (getIntent().hasExtra(CACHE_TO_DISPLAY)) {
			cache = (Cache) getIntent().getExtras().get(CACHE_TO_DISPLAY);
		}

		if (null == cache) {
			Log.e(getClass().getName(),
					"Invalid Intent given to Detail activity.");
			cache = new Cache();
		}

		updateCacheDisplayData();

		// TODO: Grey-out action buttons if null cache? Or maybe a Toast and
		// finish()?

		Button editButton = ((Button) findViewById(R.id.edit_button));
		editButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Create the Edit intent
				Intent editIntent = new Intent(GeocacheDetailActivity.this,
						GeocacheEditActivity.class);
				editIntent.putExtra(GeocacheConstants.CACHE_TO_EDIT, cache);
				startActivityForResult(editIntent, EDIT_CACHE_REQUEST);
			}
		});

		Button mapButton = ((Button) findViewById(R.id.map_button));
		mapButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent mapViewIntent = new Intent(getApplicationContext(),
						GeocacheMapViewActivity.class);
				mapViewIntent.putExtra(
						GeocacheConstants.CURRENT_LOCATION_FOR_MAP_VIEW,
						cache.getCacheCoordinate());
				startActivity(mapViewIntent);
			}
		});
	}

	private void updateCacheDisplayData() {

		((TextView) findViewById(R.id.cache_name)).setText(cache.getName());
		((TextView) findViewById(R.id.cache_description)).setText(cache
				.getDescription());
		((TextView) findViewById(R.id.cache_type)).setText(cache.getCacheType()
				.toString());
		((TextView) findViewById(R.id.cache_latitude)).setText(Double
				.toString(cache.getCacheCoordinate().getGeoPoint()
						.getLatitudeE6() / 1E6));
		((TextView) findViewById(R.id.cache_longitude)).setText(Double
				.toString(cache.getCacheCoordinate().getGeoPoint()
						.getLongitudeE6() / 1E6));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (EDIT_CACHE_REQUEST == requestCode) {
			if (RESULT_OK == resultCode) {
				// refresh the cache data, and the displayed cache data.
				if (data.hasExtra(GeocacheConstants.CACHE_TO_EDIT)) {
					cache = (Cache) data.getExtras().get(
							GeocacheConstants.CACHE_TO_EDIT);
					Intent updatedCacheIntent = new Intent();
					updatedCacheIntent.putExtra(
							GeocacheConstants.UPDATED_CACHE, cache);
					setResult(RESULT_OK, updatedCacheIntent);
				}

				if (null == cache) {
					Log.e(getClass().getName(),
							"Invalid Intent result returned to Detail activity for EDIT_CACHE_REQUEST.");
				}

				updateCacheDisplayData();
			}
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	public void processGeocacheCacheManagementRequestResponse(
			RequestStatus requestStatus, Cache cache) {
		if (requestStatus.equals(RequestStatus.SUCCESS)) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.update_success_message), 5).show();
			finish();
		} else {
			Toast.makeText(getApplicationContext(),
					getString(R.string.update_error_message), 5).show();
		}
	}
}